package cn.cdtu.tibetplan.controller;

import cn.cdtu.tibetplan.pojo.*;
import cn.cdtu.tibetplan.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class MainController {

    @Autowired
    private DemandService demandService;
    @Autowired
    private PolicyService policyService;
    @Autowired
    private DynamicsService dynamicsService;
    @Autowired
    private SchoolService schoolService;
    @Autowired
    private RecruitmentService recruitmentService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private RouteService routeService;

    @RequestMapping(value = {"/main","/"})
    public String main(Model model){
        List advers = recruitmentService.findPage(1, 7).getRows();
        model.addAttribute("advers",advers);
        List needs = demandService.findPage(1, 5).getRows();
        model.addAttribute("needs",needs);
        List questions = questionService.findPage(1, 5).getRows();
        model.addAttribute("questions",questions);
        return "main";
    }

    @ResponseBody
    @GetMapping("/?/*.html")
    public String refresh(){
        return "";
    }

    @GetMapping("/index")
    public String index(HttpSession session){
        if(session.getAttribute("userId") != null){
            return "index";
        }else {
            return "redirect:login.html";
        }
    }

    @RequestMapping("/index/policy")
    public String policy(){
        return "redirect:searchPolicy?currentPage=1&pageSize=5";
    }

    @RequestMapping("/index/searchPolicy")
    public String searchPolicy(Policy policy, int currentPage, int pageSize, Model model) {
        PageResult result = policyService.findPage(policy, currentPage, pageSize);
        PageEnd pageend = new PageEnd(result.getTotal(), result.getRows(), currentPage, pageSize);
        model.addAttribute("pageEnd", pageend);
        return "index/policyindex";
    }

    /**
     * 援藏工作
     * @return
     */
    @RequestMapping("/index/work")
    public String work(){
        return "redirect:searchWork?currentPage=1&pageSize=5";
    }

    @RequestMapping("/index/searchWork")
    public String searchWork(Dynamics dynamics, int currentPage, int pageSize, Model model) {
        List<School> schools = schoolService.findAll();
        model.addAttribute("schools", schools);
        PageResult result = dynamicsService.findPage(dynamics, currentPage, pageSize);
        PageEnd pageend = new PageEnd(result.getTotal(), result.getRows(), currentPage, pageSize);
        model.addAttribute("pageEnd", pageend);
        return "index/workindex";
    }

    @RequestMapping("/index/showWork")
    public String showWork(Long id, Model model) {
        Dynamics work = dynamicsService.findOne(id.longValue());
        model.addAttribute("work",work);
        List rows = dynamicsService.findPage(1, 10).getRows();
        model.addAttribute("works",rows);
        return "index/workshow";
    }

    /**
     * 援藏需求
     * @return
     */
    @RequestMapping("/index/need")
    public String need(){
        return "redirect:searchNeed?currentPage=1&pageSize=5";
    }
    @RequestMapping("/index/searchNeed")
    public String searchNeed(Demand demand, int currentPage, int pageSize, Model model) {
        List<School> schools = schoolService.findAll();
        model.addAttribute("schools", schools);
        PageResult result = demandService.findPage(demand, currentPage, pageSize);
        PageEnd pageend = new PageEnd(result.getTotal(), result.getRows(), currentPage, pageSize);
        model.addAttribute("pageEnd", pageend);
        return "index/needindex";
    }

    @RequestMapping("/index/showNeed")
    public String showNeed(Long id, Model model) {
        Demand need = demandService.findOne(id.longValue());
        model.addAttribute("need",need);
        List rows = demandService.findPage(1, 10).getRows();
        model.addAttribute("needs",rows);
        return "index/needshow";
    }


    /**
     * 援藏路径
     * @return
     */
    @RequestMapping("/index/process")
    public String process(){
        return "redirect:searchProcess?currentPage=1&pageSize=5";
    }
    @RequestMapping("/index/searchProcess")
    public String searchProcess(Route route, int currentPage, int pageSize, Model model) {
        PageResult result = routeService.findPage(route, currentPage, pageSize);
        PageEnd pageend = new PageEnd(result.getTotal(), result.getRows(), currentPage, pageSize);
        model.addAttribute("pageEnd", pageend);
        return "index/processindex";
    }

    @RequestMapping("/index/showProcess")
    public String showProcess(Long id, Model model){
        Route route = routeService.findOne(id);
        final String[] noods = route.getInfo().split("-");
        model.addAttribute("routeName",route.getTitle());
        model.addAttribute("noods",noods);
        return "/index/processshow";
    }

    /**
     * 援藏高校
     * @return
     */
    @RequestMapping("/index/school")
    public String school(){
        return "redirect:searchSchool?currentPage=1&pageSize=5";
    }

    @RequestMapping("/index/searchSchool")
    public String searchSchool(SchoolWithBLOBs school, int currentPage, int pageSize, Model model) {
        List<School> schools = schoolService.findAll();
        model.addAttribute("schools", schools);
        PageResult result = schoolService.findPage(school, currentPage, pageSize);
        PageEnd pageend = new PageEnd(result.getTotal(), result.getRows(), currentPage, pageSize);
        model.addAttribute("pageEnd", pageend);
        return "index/schoolindex";
    }

    @RequestMapping("/index/showSchool")
    public String showSchool(Long id, Model model) {
        PageResult pageResult = schoolService.findPage(1, 5);
        List rows = pageResult.getRows();
        model.addAttribute("schools", rows);
        SchoolWithBLOBs school = schoolService.show(id.longValue());
        model.addAttribute("school", school);
        return "index/schoolshow";
    }

    /**
     * 人员招募
     * @param model
     * @return
     */
    @RequestMapping("/index/adver")
    public String adver(Model model){
        List<Recruitment> list = recruitmentService.findAll();
        model.addAttribute("advers",list);
        return "redirect:searchAdver?currentPage=1&pageSize=5";
    }

    @RequestMapping("/index/searchAdver")
    public String searchAdver(Recruitment recruitment, int currentPage, int pageSize, Model model) {
        List<School> schools = schoolService.findAll();
        model.addAttribute("schools", schools);
        PageResult result = recruitmentService.findPage(recruitment, currentPage, pageSize);
        PageEnd pageend = new PageEnd(result.getTotal(), result.getRows(), currentPage, pageSize);
        model.addAttribute("pageEnd", pageend);
        return "index/adverindex";
    }

    @RequestMapping("/index/adverShow")
    public String adverShow(Long id, Model model) {
        PageResult pageResult = recruitmentService.findPage(1, 5);
        List rows = pageResult.getRows();
        model.addAttribute("advers", rows);
        Recruitment adver = recruitmentService.findOne(id.longValue());
        model.addAttribute("adver", adver);
        return "index/advershow";
    }
}
